<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改课程附件')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-courseAppendix-edit" th:object="${courseAppendix}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label">文件名称：</label>
                <div class="col-sm-8">
                    <input id="title" name="title" th:field="*{title}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label"></label>
                <div class="col-sm-8">
                    <input type="file" name="upload" id="upload" style="display: none;"/>
                    <a class="btn btn-success" onclick="onFile()" >
                        <i class="fa fa-plus"></i> 添加附件
                    </a>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">链接：</label>
                <div class="col-sm-8">
                    <input id="url" name="url" th:field="*{url}" class="form-control" type="text">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/courseAppendix";
        $("#form-courseAppendix-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-courseAppendix-edit').serialize());
            }
        }

        $("input[name='created']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        function onFile() {
            $('#upload').click();
        }

        $('#upload').on('change', function () {
            $.modal.loading("拼命上传中，请稍候...");
            var farmdate = new FormData();
            farmdate.append("file", $('#upload')[0].files[0]);

            var file = $('#upload').val();
            var pos = file.lastIndexOf("\\");
            var fileName = file.substring(pos+1);

            $('#title').val(fileName);

            $.ajax({
                type: "post",
                url: "/system/courseContent/importImg",
                data: farmdate,
                contentType: false,
                processData: false,
                success: function (date) {
                    $('#url').val(date);
                    $.modal.closeLoading();
                }
            })
        });

    </script>
</body>
</html>